#!/usr/bin/perl

my $SYSVERS = "$ARGV[3]"."/System/Library/CoreServices/SystemVersion.plist";
my $TARGET  = "$ARGV[3]";
my $PKG_RESOURCES = "\Q$ARGV[0]";

##################

sub CheckVersion
{
    my $path            = $_[0];
    my $version         = $_[1];
    my $keyName         = $_[2];
    my $operator        = $_[3];

    if (! -e $path) {
        return 0;
    }

    if (!$operator) {
        $operator = "==";
    }

    my $oldSeperator = $/;
    $/ = \0;

    open( PLIST, "$path") || do {
        return 0;
    };

    $plistData = <PLIST>;
    $plistData =~ /<dict>(.*?)<\/dict>/gis;

    @items = split(/<key>/, $plistData);

    shift @items;
    foreach $item (@items) {
        $item =~ /(.*?)<\/key>.*?<string>(.*?)<\/string>/gis;
        $versiondata{ $1 } = $2;
    }

    close(PLIST);

    $/ = $oldSeperator;

    @theVersionArray = split(/\./, $versiondata{$keyName});
    for ($i = 0; $i < 3; $i++) {
        if(!$theVersionArray[$i]) {
            $theVersionArray[$i] = '0';
        }
    }

    @versionArray = split(/\./, $version);
    
    my $actualVersion;

    for ($i = 0; $i < 3; $i++) {
        if (($theVersionArray[$i] != $versionArray[$i]) or ($i == 2)) {

            $actualVersion = $theVersionArray[$i];
            $version = $versionArray[$i];

            last;
        }
    }

    my $expression = '$actualVersion ' . $operator . ' $version';
    if( eval ($expression) )
    {
        return 1;
    }
    else
    {
        return 0;
    }

}

#######################################

if (CheckVersion("$SYSVERS", "10.5", "ProductVersion", "<")) {
	# pre-Leopard systems
	system("/bin/rm", "-f", "$TARGET/System/Library/LaunchDaemons/com.apple.IIDCAssistant.plist");
	system("/bin/rm", "-f", "$TARGET/System/Library/LaunchDaemons/com.apple.mio.IIDCVideoAssistant.plist");
	system("/bin/rm", "-f", "$TARGET/System/Library/LaunchDaemons/com.apple.mio.VDCAssistant.plist");
}

else {
	# Leopard (and later)
	system("/bin/rm", "-f", "$TARGET/private/etc/mach_init.d/IIDCAssistant.plist");
	system("/bin/rm", "-f", "$TARGET/private/etc/mach_init.d/IIDCVideoAssistant.plist");
	system("/bin/rm", "-f", "$TARGET/private/etc/mach_init.d/VDCAssistant.plist");
}

#######################################

my $runnerPID = getppid();
my $installerPID = "";
my $uid = "";
my $uname = "";

open( PSOUT, "/bin/ps -axww -o pid,ppid -p $runnerPID |" );
while( <PSOUT> ) {
	my @fields = split '\s+', $_;
	if ("$fields[1]" eq "$runnerPID") {
		$installerPID = $fields[2];
	}
}
close( PSOUT );

if ("$installerPID" ne "") {
	open( PSOUT, "/bin/ps -axww -o pid,ruid -p $installerPID |" );
	while( <PSOUT> ) {
		my @fields = split '\s+', $_;
		if ("$fields[1]" eq "$installerPID") {
			$uid = $fields[2];
		}
	}
	close( PSOUT );
}

if ("$uid" ne "") {
	open( PSOUTA, "/usr/bin/id -p $uid |" );
	while( <PSOUTA> ) {
		my @fields = split '\s+', $_;
		if ("$fields[0]" eq "uid") {
			$uname = $fields[1];
		}
	}
	close( PSOUTA );
}

###################################################################

# Remove old unneeded quicktime pieces
my $CLEANUP_SCRIPT = "$PKG_RESOURCES" . "/Contents/Resources/cleancruft";
$cmd = "$CLEANUP_SCRIPT $PKG_RESOURCES \"$ARGV[1]\" \"$ARGV[2]\" \"$ARGV[3]\"";
system( "$cmd" );

########	
# Stuff the installation info into the preferences for QT Updates
my $INSTALL_INFO= "$PKG_RESOURCES" . "/Contents/Resources/installinfo";
my $cmd = "$INSTALL_INFO -postinstall -volume \"$ARGV[2]\" -type full";
#print STDOUT "INSTALL_INFO: $cmd\n";
system( "$cmd" );

########

# Configure the browser mime types based on the installed QuickTime
my $CONFIGURE_MIME_TYPES= "$PKG_RESOURCES" . "/Contents/Resources/ConfigureMimeTypes";
my $cmd = "$CONFIGURE_MIME_TYPES -rescan -verbose";
if ("$uname" ne "") {
	$cmd = "sudo -u $uname $CONFIGURE_MIME_TYPES -rescan -verbose";
}
#print STDOUT "CONFIGURE_MIME_TYPES: $cmd\n";
system( "$cmd" );

########

# Remove old receipts
system ("/bin/rm -rf \"$TARGET/Library/Receipts/QuickTime702.pkg\"");
system ("/bin/rm -rf \"$TARGET/Library/Receipts/QuickTime703.pkg\"");
system ("/bin/rm -rf \"$TARGET/Library/Receipts/QuickTime704\"*\".pkg\"");
system ("/bin/rm -rf \"$TARGET/Library/Receipts/QuickTime71.pkg\"");
system ("/bin/rm -rf \"$TARGET/Library/Receipts/QuickTime71Seed.pkg\"");
system ("/bin/rm -rf \"$TARGET/Library/Receipts/QuickTime711.pkg\"");
system ("/bin/rm -rf \"$TARGET/Library/Receipts/QuickTime712.pkg\"");
system ("/bin/rm -rf \"$TARGET/Library/Receipts/QuickTime713.pkg\"");
system ("/bin/rm -rf \"$TARGET/Library/Receipts/QuickTime715.pkg\"");
system ("/bin/rm -rf \"$TARGET/Library/Receipts/QuickTime716.pkg\"");
system ("/bin/rm -rf \"$TARGET/Library/Receipts/QuickTime71?Seed.pkg\"");
system ("/bin/rm -rf \"$TARGET/Library/Receipts/QuickTime720.pkg\"");
system ("/bin/rm -rf \"$TARGET/Library/Receipts/QuickTime730.pkg\"");
system ("/bin/rm -rf \"$TARGET/Library/Receipts/QuickTime740.pkg\"");
system ("/bin/rm -rf \"$TARGET/Library/Receipts/QuickTime741.pkg\"");
system ("/bin/rm -rf \"$TARGET/Library/Receipts/QuickTime745.pkg\"");
system ("/bin/rm -rf \"$TARGET/Library/Receipts/QuickTime75.pkg\"");

###########

#remove files in cleanup list (for Tiger)
my $Delete = "$PKG_RESOURCES" . "/Contents/Resources/deleteomatic";
my $TIGER_DELETED_FILES="$PKG_RESOURCES" . "/Contents/Resources/tigercleanup-list-post";
if((CheckVersion( "$SYSVERS", "10.4", "ProductVersion", ">=" )) && (CheckVersion( "$SYSVERS", "10.5", "ProductVersion", "<" ))) {
	system ("$Delete \"$ARGV[2]\" $TIGER_DELETED_FILES"); 
}

####cleanup is not absent in SEED package#######

my $CLEAN_SCRIPT = "$PKG_RESOURCES" . "/Contents/Resources/cleanup";
$cmd = "$CLEAN_SCRIPT $PKG_RESOURCES \"$ARGV[1]\" \"$ARGV[2]\" \"$ARGV[3]\"";
if(-e $CLEAN_SCRIPT) {
	system( "$cmd" );
}

# Removed exit(0); because engineering builds wants to concat more stuff to the end of this file
